import Mock from 'mockjs';
import setupMock, {
  successResponseWrap,
  failResponseWrap,
} from '@/utils/setup-mock';

import { MockParams } from '@/types/mock';

setupMock({
    setup() {
        // 获取页面配置及大小征象
        Mock.mock(new RegExp('/anno/series_result/read'), (params: MockParams) => {
            const { id } = JSON.parse(params.body || '{}');
            if (id !== 13334) {
                return failResponseWrap(null, '请求id错误', 500);
            };
            return successResponseWrap({
                algType: null,
                algVersionId: null,
                annotationItemResultList: [
                    {
                        annotationItemId: 9385,
                        formComponentId: 1073,
                        id: 68249,
                        imageAnnotationResultId: 0,
                        seriesAnnotationResultId: 13334,
                        result: null
                    }
                ],
                id: 13334,
                imageAnnotationResultList: [
                    {
                        annotationItemResultList: [
                            {
                                annotationItemId: 9402,
                                formComponentId: 1073,
                                id: 68250,
                                imageAnnotationResultId: 407231,
                                seriesAnnotationResultId: 13334,
                                result: null
                            }
                        ],
                        defaultAnno: false,
                        fromIarId: 0,
                        id: 407231,
                        imageAnnotationId: 9988,
                        imageAnnotationToolId: 11983,
                        imageAnnotationToolType: 'RECTANGLE',
                        imageAnnotationType: 'PDL1_1',
                        isTmpResult: false,
                        originResult: {
                            data: { 
                                point1: { 
                                    x: 18635.294921875,
                                    y: 9519.328125
                                },
                                point2: { 
                                    x: 27670.587890625,
                                    y: 18635.2939453125
                                }
                            },
                            version: 1
                        },
                        result: {
                            data: { 
                                point1: { 
                                    x: 18635.294921875,
                                    y: 9519.328125
                                },
                                point2: { 
                                    x: 27670.587890625,
                                    y: 18635.2939453125
                                }
                            },
                            version: 1
                        },
                        resultType: 2
                    }
                ],
                projectId: 3481,
                taskId: 4537,
                userId: 5287
            });
        });

        // 初始化获取页面大小征象
        Mock.mock(new RegExp('/project/anno/read'), (params: MockParams) => {
            const { taskId } = JSON.parse(params.body || '{}');
            if (!taskId) {
                failResponseWrap(null, '接口缺少参数', 500);
            }
            if (taskId !== 3481) {
                return failResponseWrap(null, '请求错误', 500);
            };
            return successResponseWrap({
                annotationItemList: [
                    {
                        componentData: [
                            { 
                                "code": "1",
                                "text": "肋骨",
                                "child": [
                                    { 
                                        "code": "1",
                                        "text": "1"
                                    },
                                    {
                                        "code": "2",
                                        "text": "2"
                                    },
                                    {
                                        "code": "3",
                                        "text": "3"
                                    },
                                    {
                                        "code": "4",
                                        "text": "4"
                                    },
                                    {
                                        "code": "5",
                                        "text": "5"
                                    },
                                    {
                                        "code": "6",
                                        "text": "6"
                                    },
                                    {
                                        "code": "7",
                                        "text": "7"
                                    },
                                    {
                                        "code": "8",
                                        "text": "8"
                                    },
                                    {
                                        "code": "9",
                                        "text": "9"
                                    },
                                    {
                                        "code": "10",
                                        "text": "10"
                                    },
                                    {
                                        "code": "11",
                                        "text": "11"
                                    },
                                    {
                                        "code": "12",
                                        "text": "12"
                                    },
                                    {
                                        "code": "13",
                                        "text": "13"
                                    }
                                ]
                            },
                            {
                                "code": "2",
                                "text": "胸骨",
                                "child": [
                                    {
                                        "code": "1",
                                        "text": "左"
                                    },
                                    {
                                        "code": "2",
                                        "text": "右"
                                    }
                                ]
                            }
                        ],
                        componentCode: 1076,
                        componentId: 1076,
                        componentName: '骨折部位',
                        componentParameter: {
                            "isdefault": false
                        },
                        componentType: 'child_select',
                        formComponentId: 1076,
                        id: 9383,
                        imageAnnotationId: 0,
                        projectId: 3481,
                        optional: true,
                        required: true,
                        sequence: 10,
                        tagList: [
                            {
                                category: 'COMPONENT',
                                id: 45,
                                text: '骨折'
                            }
                        ]
                    },
                    {
                        componentData: [
                            {
                                "text": "左优势型",
                                "code": "0"
                            },
                            {
                                "text": "右优势型",
                                "code": "1"
                            },
                            {
                                "text": "平衡型",
                                "code": "2"
                            },
                            {
                                "text": "其他",
                                "code": "3"
                            }
                        ],
                        componentCode: 1076,
                        componentId: 1076,
                        componentName: '冠脉优势型',
                        componentParameter: {
                            "isdefault": false
                        },
                        componentType: 'radiobox',
                        formComponentId: 1076,
                        id: 9384,
                        imageAnnotationId: 0,
                        projectId: 3481,
                        optional: true,
                        sequence: 10,
                        required: true,
                        tagList: null
                    },
                    {
                        componentData: null,
                        componentCode: 1076,
                        componentId: 1076,
                        componentName: '备注审核医生',
                        componentParameter: {
                            "texttype": 'text'
                        },
                        componentType: 'text',
                        formComponentId: 1076,
                        id: 9385,
                        imageAnnotationId: 0,
                        projectId: 3481,
                        optional: false,
                        sequence: 10,
                        required: false,
                        tagList: null
                    },
                    {
                        componentData: [
                            {
                                "text": "A分类",
                                "code": "1"
                            },
                            {
                                "text": "B分类",
                                "code": "2"
                            },
                            {
                                "text": "C分类",
                                "code": "3"
                            }
                        ],
                        componentCode: 1076,
                        componentId: 1076,
                        componentName: '标注分类',
                        componentParameter: {
                            "isdefault": false
                        },
                        componentType: 'select',
                        formComponentId: 1076,
                        id: 9386,
                        imageAnnotationId: 0,
                        projectId: 3481,
                        optional: true,
                        sequence: 40,
                        required: true,
                        tagList: null
                    },
                    {
                        componentData: [
                            {
                                "text": "是(的测试)",
                                "code": "1"
                            },
                            {
                                "text": "不是(测试)",
                                "code": "2"
                            }
                        ],
                        componentCode: 1076,
                        componentId: 1076,
                        componentName: '是否有骨折审核医生',
                        componentParameter: {
                            "hasnull": true
                        },
                        componentType: 'checkbox',
                        formComponentId: 1076,
                        id: 9387,
                        imageAnnotationId: 0,
                        projectId: 3481,
                        optional: false,
                        sequence: 50,
                        required: false,
                        tagList: null
                    }
                ],
                imageAnnotationList: [
                    {
                        annotationItemList: [
                            {
                                componentData: null,
                                componentCode: 1076,
                                componentId: 1076,
                                componentName: '备注审核医生',
                                componentParameter: {
                                    "texttype": 'text'
                                },
                                componentType: 'text',
                                formComponentId: 1076,
                                id: 9385,
                                imageAnnotationId: 0,
                                projectId: 3481,
                                optional: false,
                                sequence: 10,
                                required: false,
                                tagList: null
                            }
                        ],
                        color: '#d8c621',
                        defaultAnno: false,
                        toolList: [
                            {
                                id: 11975,
                                imageAnnotationId: 9988,
                                type: 'C_POINT_MARK',
                            }
                        ],
                        type: 'PDL1_1'
                    }
                ],
                inspectSee: false,
                isLungPdf: false,
                isYayAttributes: false,
                largeFigure: false,
                seriesImgFileType: 15
            });
        })

        // 初始获取标注序列
        Mock.mock(new RegExp('/test/anno/result/series/get'), (params: MockParams) => {
            const { taskId } = JSON.parse(params.body || '{}');
            if (!taskId) {
                failResponseWrap(null, '接口缺少参数', 500);
            }
            return successResponseWrap({
                arList: [
                    {
                        arId: 1,
                        seriesUid: '123'
                    }
                ]
            })
        })

        // 初始获取征象，工具
        Mock.mock(new RegExp('/test/anno/setting/read'), (params: MockParams) => {
            const { taskId } = JSON.parse(params.body || '{}');
            if (!taskId) {
                failResponseWrap(null, '接口缺少参数', 500);
            }
            return successResponseWrap({
                seriesCompList: [
                    {
                        componentId: 1,
                        required: true
                    },
                    {
                        componentId: 2,
                        required: true
                    },
                    {
                        componentId: 3,
                        required: true
                    },
                    {
                        componentId: 4,
                        required: true
                    },
                    {
                        componentId: 5,
                        required: true
                    }
                ],
                lesionList: [
                    {
                        lesion: '病灶1',
                        color: '#448E97',
                        toolList: ['rect', 'ellipse'],
                        imageCompList: [
                            {
                                componentId: 1,
                                required: true
                            },
                            {
                                componentId: 3,
                                required: false
                            }
                        ]
                    },
                    {
                        lesion: '病灶2',
                        color: '#F40',
                        toolList: ['rect', 'ellipse'],
                        imageCompList: [
                            {
                                componentId: 2,
                                required: true
                            },
                            {
                                componentId: 4,
                                required: true
                            }
                        ]
                    },
                    {
                        lesion: '病灶3',
                        color: '#F0F',
                        toolList: ['ellipse'],
                        imageCompList: [
                            {
                                componentId: 5,
                                required: true
                            }
                        ]
                    }
                ],
                discardCodeList: null,
                defaultWindow: null,
                findings: true,
                diagnosis: true,
                showYay: false,
                showReport: true,
                imageLayout: '1'
            })
        })

        // 查询获取征象，工具
        Mock.mock(new RegExp('/test/anno/result/read'), (params: MockParams) => {
            const { arId } = JSON.parse(params.body || '{}');
            if (!arId) {
                failResponseWrap(null, '接口缺少参数', 500);
            }
            return successResponseWrap({
                yay: null,
                seriesCompResultList: [
                    {
                        id: 1,
                        componentId: 1,
                        result: '5'
                    },
                    {
                        id: 2,
                        componentId: 2,
                        result: '4'
                    },
                    {
                        id: 3,
                        componentId: 3,
                        result: '这是一个备注'
                    },
                    {
                        id: 4,
                        componentId: 4,
                        result: ['0', '5']
                    }
                ],
                lesionResultList: [
                    {
                        id: 1,
                        lesion: '病灶1',
                        color: '#448E97',
                        tool: 'rect',
                        result: "{data: {point1: {x: 89, y: 109}, point2: {x: 186, y: 205}}, acZoom: 0.34165111940298504, version: 1}",
                        resultType: 2,
                        imageCompResultList: [
                            {
                                id: 1,
                                componentId: 1,
                                result: '5'
                            },
                            {
                                id: 2,
                                componentId: 3,
                                result: '这是一个备注'
                            }
                        ]
                    }
                ],
            })
        })

        // 新建保存标注及更新标注
        Mock.mock(new RegExp('/test/anno/lesion/result/point'), (params: MockParams) => {
            if (params.type === 'POST') {
                const { arId, lesion, tool, result, color } = JSON.parse(params.body || '{}');
                if (!arId && !lesion && !tool && !result) {
                    failResponseWrap(null, '接口缺少参数', 500);
                }
                return successResponseWrap({
                    alrId: Math.floor((Math.random() * 10) * 100000)
                })
            } else {
                const { alrId, result, color } = JSON.parse(params.body || '{}');
                if (!alrId && !result) {
                    failResponseWrap(null, '接口缺少参数', 500);
                }
                return successResponseWrap(null);
            }
        })

        // 删除标注
        Mock.mock(new RegExp(`/test/anno/lesion/result/`), (params: MockParams) => {
            // const { alrId } = JSON.parse(params.body || '{}');
            if (params.type === 'DELETE') {
                const urls = params.url.split('/');
                const alrId = urls[urls.length - 1];
                if (alrId) {
                    return successResponseWrap(null);
                }
            }
            return failResponseWrap(null, '删除失败', 500);
        })

        // 新建保存大小征象及更新征象
        Mock.mock(new RegExp('/test/anno/component/result'), (params: MockParams) => {
            if (params.type === 'POST') {
                const { arId, acrList } = JSON.parse(params.body || '{}');
                if (!arId && acrList.length === 0) {
                    failResponseWrap(null, '接口缺少参数', 500);
                }
                return successResponseWrap({
                    acrList: [
                        {
                            alrId: 1,
                            acrId: 1,
                            componentId: 1
                        }
                    ]
                })
            } else {
                const { acrId, result } = JSON.parse(params.body || '{}');
                if (!acrId && !result) {
                    failResponseWrap(null, '接口缺少参数', 500);
                }
                return successResponseWrap(null);
            }
        })

        // 阴阳性处理
        Mock.mock(new RegExp('/test/anno/result/yay'), (params: MockParams) => {
            const { arId, yay } = JSON.parse(params.body || '{}');
            if (!arId && !yay) {
                failResponseWrap(null, '接口缺少参数', 500);
            }
            return successResponseWrap(null);
        })

        // 最终提交
        Mock.mock(new RegExp('/test/anno/result/submit'), (params: MockParams) => {
            const { arIdList, status, discardCode, discardRemark } = JSON.parse(params.body || '{}');
            if (arIdList.length === 0 && !status) {
                failResponseWrap(null, '接口缺少参数', 500);
            }
            if (status === 2) {
                return successResponseWrap('保存成功！');
            } else {
                return successResponseWrap('当前标注标注废片成功！');
            }
        })
    }
});